package com.haojiang.mh.remote.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.haojiang.mh.R
import com.haojiang.mh.remote.bean.BleRssiDevice
import com.haojiang.mh.remote.bean.ConnectValue
import com.haojiang.mh.remote.callback.IBleItemClickLisenter
import com.haojiang.mh.remote.enums.EConnect
import com.haojiang.mh.remote.ui.dialog.ModifyDialogFrag
import com.haojiang.mh.remote.utils.BleUtil.Companion.ble
import com.haojiang.mh.remote.utils.Util.Companion.clickItemDelay
import com.haojiang.mh.remote.utils.Util.Companion.playSound
import com.haojiang.mh.remote.widget.BleGroupItemView
import kotlinx.android.synthetic.main.item_ble_group.view.*

class BleGroupDialogFragAdapter(context: Context, list: MutableList<BleRssiDevice>) :
    RecyclerView.Adapter<BleGroupDialogFragAdapter.BleDialogFragViewHolder>() {

    private var dataList: MutableList<BleRssiDevice> = list
    val viewList = ArrayList<BleGroupItemView>()
    var iOnBleItemClickLisenter: IBleItemClickLisenter? = null
    var groupType = EConnect.CONNECT_GROUP_SINGLE.data
    var fragmentActivity = context as FragmentActivity

    private val onSingleItemClick = object : View.OnClickListener {
        override fun onClick(v: View?) {
            val itemView = v as BleGroupItemView
            ble.stopScan()
            viewList.forEach {
                if (it.bleRssiDevice!!.bleAddress.equals(itemView.bleRssiDevice!!.bleAddress)) {
                    it.isSelect = true
                    it.ivSelector.setImageResource(R.drawable.ic_select)
                } else {
                    it.isSelect = false
                    it.ivSelector.setImageResource(R.drawable.ic_unselect)
                }
            }
        }
    }

    private val onMutipleItemClick = object : View.OnClickListener {
        override fun onClick(v: View?) {
            val itemView = v as BleGroupItemView
            ble.stopScan()
            viewList.forEach {
                if (it.bleRssiDevice!!.bleAddress.equals(itemView.bleRssiDevice!!.bleAddress)) {
//                                it.isSelect = !it.isSelect
                    if (it.isSelect) {
                        it.isSelect = false
                        it.ivSelector.setImageResource(R.drawable.ic_unselect)
                    } else {
                        it.isSelect = true
                        it.ivSelector.setImageResource(R.drawable.ic_select)
                    }
                }
            }
        }
    }

    //初始化数据
    fun initData(groupType: Int) {
        this.groupType = groupType
        this.viewList.forEach {
            it.isSelect = false
        }
        this.viewList.clear()
        this.notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BleDialogFragViewHolder {
        return BleDialogFragViewHolder(BleGroupItemView(parent.context))       //BleItemView是layout文件
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: BleDialogFragViewHolder, position: Int) {
        val bleRssiDevice = dataList.get(position)
        val itemView = holder.itemView as BleGroupItemView

        if (!viewList.contains(itemView)) {
            itemView.bleRssiDevice = bleRssiDevice
            viewList.add(itemView)
            itemView.setData(bleRssiDevice) //设置rssi和设备别名
            //数据选择界面也跟着选择
//            LogUtil.e(itemView.bleRssiDevice!!.bleName + "=选择=" + itemView.isSelect)
            if (itemView.isSelect) {
                itemView.ivSelector.setImageResource(R.drawable.ic_select)
            } else {
                itemView.ivSelector.setImageResource(R.drawable.ic_unselect)
            }

            //修改设备名称
            itemView.ivModify.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    ble.stopScan()
                    fragmentActivity.run {
                        ModifyDialogFrag.show(
                            fragmentActivity,
                            bleRssiDevice.bleAddress,
                            bleRssiDevice.bleName,
                            bleRssiDevice.deviceAlias!!
                        )
                    }
//                    EventBus.getDefault().post(GroupModifyEvent(BleGroupDialogFragAdapter::class.java.simpleName, bleRssiDevice))
                }
            })

            //l连接设备按钮
            itemView.tvTest.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    clickItemDelay(v!!, 3000)
                    ble.stopScan()
                    playSound()
                    val bleDeivceList = ArrayList<BleRssiDevice>()
                    bleDeivceList.add(dataList.get(position))
                    iOnBleItemClickLisenter?.onClick(bleDeivceList, EConnect.CONNECT_GROUP_TEST.data)
                }
            })

            //如果是组选择子选项
            setSelectType(itemView)
        }
    }


    private fun setSelectType(itemView: BleGroupItemView) {
        when (groupType) {
            EConnect.CONNECT_GROUP_SINGLE.data,
            EConnect.CONNECT_GROUP_SPLIT.data -> {
                //单个选择
//                itemView.ivSelector.setOnClickListener(onSingleItemClick)
                itemView.setOnClickListener(onSingleItemClick)
            }

            EConnect.CONNECT_GROUP_MULTIPLE.data -> {
                //多个选择
//                itemView.ivSelector.setOnClickListener(onMutipleItemClick)
                itemView.setOnClickListener(onMutipleItemClick)

//                itemView.ivSelector.setOnClickListener {
//                    viewList.forEach {
//                        if (it.equals(itemView)) {
//                            it.ivSelector.setImageResource(R.drawable.ic_select)
//                        } else {
//                            it.ivSelector.setImageResource(R.drawable.ic_unselect)
//                        }
//                    }
//                }
//                itemView.cbSelect.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener {
//                    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
//                        bleRssiDevice.isSelect = isChecked
//                    }
//                })
//                if (itemView.cbSelect.visibility == View.VISIBLE) {
//                    itemView.setOnClickListener(View.OnClickListener {
//                        bleRssiDevice.isSelect = !bleRssiDevice.isSelect
//                        itemView.cbSelect.isChecked = bleRssiDevice.isSelect
//                    })
//                }
            }
        }
    }


    fun dataRefresh() {
//        Collections.sort(dataList)    //排序
        dataList.clear()
        notifyDataSetChanged()
    }

    fun modifyItem(bleAddress: String, deviceAlias: String) {
        for (i in 0 until viewList.size) {
            if (viewList.get(i).bleRssiDevice!!.bleAddress.equals(bleAddress)) {
                viewList.get(i).tvDevName.setText(deviceAlias)
                break
            }
        }
//        for (i: Int in 0..dataList.size) {
////            LogUtil.e(dataList.get(i).bleName + "=??=" + dataList.get(i).deviceAlias + "==" + deviceAlias)
//            if (dataList.get(i).bleAddress.equals(bleAddress)) {
//                dataList.get(i).deviceAlias = deviceAlias
//                notifyItemChanged(i)
//                break
//            }
//        }
    }

    fun getSelectList(): ArrayList<ConnectValue> {
        val groupValueLIst = ArrayList<ConnectValue>()
        viewList.forEach {
            if (it.isSelect) {
                groupValueLIst.add(ConnectValue(it.bleRssiDevice!!))
//                LogUtil.e("选择=" + viewList.size + "==" + it.bleAddress + "=" + it.deviceAlias)
            }
        }
        return groupValueLIst
    }


    class BleDialogFragViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    }
}